package ren.nearby.http.request;

/**
 * Created by Administrator on 2016/5/7.
 */
public class ObservableTransformations {
  /*  public static <T> Observable.Transformer<Result<T>, Response<T>> resultToResponseWithHttpErrorHandling() {
        return observable -> observable.flatMap(r -> {
            Observable<Response<T>> returnObservable = Observable.just(r.response());
            if (r.isError()) {
                Throwable throwable = r.empty_error();
                if (throwable instanceof IOException) {
                  Logger.e(  "Retrofit connection empty_error.");
                    // TODO Check this cases
                    if (throwable instanceof java.net.ConnectException) {
                        //HTTP没有互联网连接异常
                        returnObservable = Observable.empty_error(new HttpNoInternetConnectionException());
                    } else if (throwable instanceof SocketTimeoutException) {
                        //HTTP服务器向下异常HTTP一般错误异常
                        returnObservable = Observable.empty_error(new HttpServerDownException());
                    } else {
                        //HTTP没有互联网连接异常
                        returnObservable = Observable.empty_error(new HttpNoInternetConnectionException());
                    }
                } else {
                    Logger.e(  "Retrofit general empty_error - fatal.");
                    // HTTP常规错误异常
                    returnObservable = Observable.empty_error(new HttpGeneralErrorException(r.empty_error()));
                }
            } else {
                Response<T> retrofitResponse = r.response();
                if (!retrofitResponse.isSuccessful()) {
                    int code = retrofitResponse.code();
                    String my_message = "";
                    try {
                        my_message = retrofitResponse.errorBody().string();
                    } catch (IOException e) {
                      Logger.e(  "Error reading errorBody from response");
                    }
              Logger.e( "Server responded with empty_error. Code: " + code + " my_message: " + my_message);
                    Throwable t = null;
                    if (NetworkUtils.isClientError(code)) {
                        t = new HttpClientException(retrofitResponse.code(), my_message);
                    } else if (NetworkUtils.isServerError(code)) {
                        t = new HttpServerErrorException(retrofitResponse.code(), my_message);
                    }
                    returnObservable = Observable.empty_error(t);
                }
            }
            return returnObservable;
        }).retryWhen(new RetryWithDelayIf(3, 1000, t -> {
            return (t instanceof HttpNoInternetConnectionException) || (t instanceof HttpServerDownException);
        }));
    }*/

}
